package com.example.administrator.energyida.adapter;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextSwitcher;
import android.widget.TextView;

import com.example.administrator.energyida.MainActivity;
import com.example.administrator.energyida.R;
import com.example.administrator.energyida.activity.ManualAddActivity;
import com.example.administrator.energyida.fragment.ByDayFragment;

import java.util.List;

public class ItemListviewByDayAdapter extends BaseAdapter {

    private List energyList;
    private Context mContext;

    public ItemListviewByDayAdapter(List energyList, Context mContext) {
        this.energyList = energyList;
        this.mContext = mContext;
    }

    public interface Callback {
        public void change(View view);
    }

    @Override
    public int getCount() {
        return energyList.size();
    }

    @Override
    public Object getItem(int position) {
        return energyList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_listview_byday, parent, false);
            viewHolder = new ViewHolder();
            viewHolder.textViewEnergy = convertView.findViewById(R.id.addByDay_listView_energy);
            viewHolder.editTextData = convertView.findViewById(R.id.addByDay_listView_editTextData);
            viewHolder.editTextData.setTag(position);
//            viewHolder.editTextData.addTextChangedListener(new TextSwitcher(this.ViewHolder));


            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.textViewEnergy.setText(energyList.get(position).toString());
        return convertView;
    }

    class ViewHolder {
        private TextView textViewEnergy;
        private EditText editTextData;

    }



    class TextSwitcher implements TextWatcher {

        private ViewHolder viewHolder;

        ManualAddActivity manualAddActivity = new ManualAddActivity();

        public TextSwitcher(ViewHolder viewHolder) {
            this.viewHolder = viewHolder;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            int position = (int) viewHolder.editTextData.getTag();

            manualAddActivity.saveEditData(position, s.toString());
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    }
}
